//
//  PulishRentInfoVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/26.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "PulishRentInfoVC.h"
#import "PublishRentInfoCell.h"
#import "PublishRentFirstCell.h"
#import "PublishRentSetionCell.h"
#import "PublishRentImagesCell.h"
#import "PublishRentDesCell.h"
#import <LEEAlert/LEEAlert.h>
#import "SelectedListView.h"
#import "HouseParameSub.h"
#import "SelectFeatureVC.h"

#import "SelectPopoDelegate.h"

#import <WXApi.h>
#import <AlipaySDK/AlipaySDK.h>
@interface PulishRentInfoVC ()


@property (nonatomic, strong) NSMutableArray<NSDictionary *> *dateArray;

@property (nonatomic, strong) SelectPopoDelegate<UIViewControllerTransitioningDelegate> *animedDelegate;


/** <#note#> */
@property (nonatomic, strong) NSString *payMoney;

/** <#note#> */
@property (nonatomic, strong) NSString *payOrderNumber;


@property (nonatomic, strong) NSString *payType;


/** <#note#> */
@property (nonatomic, strong) NSMutableArray *images;
@end

@implementation PulishRentInfoVC

- (SelectPopoDelegate<UIViewControllerTransitioningDelegate> *)animedDelegate{
    
    if (_animedDelegate == nil) {
        
        _animedDelegate = [[SelectPopoDelegate alloc]init];
    }
    
    return  _animedDelegate;
}


- (NSMutableArray<NSDictionary *> *)dateArray{
    
    if (_dateArray == nil) {
        
        _dateArray = [[NSMutableArray alloc]init];
        
        for (ProvinceModel *province in myViewModel.areas) {
            NSMutableDictionary *mutDictionary = [[NSMutableDictionary alloc]init];
            mutDictionary[@"name"] = province.province_name;
            NSMutableArray<NSMutableDictionary *> *tempArray = [[NSMutableArray alloc]init];
            
            for (CitysModel *city in province.citys) {
                NSMutableDictionary *subDictionary = [[NSMutableDictionary alloc]init];
                subDictionary[@"name"] = city.city_name;
                NSMutableArray *subArray = [[NSMutableArray alloc]init];
                
                for (DistrictsModel *district in city.districts) {
                    
                    [subArray addObject:district.district_name];
                    
                }
                
                subDictionary[@"sub"] = subArray;
                
                [tempArray addObject:subDictionary];
            }
            
            
            mutDictionary[@"sub"] = tempArray;
            
            [_dateArray addObject:mutDictionary];
        }
        
        
        
    }
    
    
    return _dateArray;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.estimatedRowHeight = 100;
    
    [self loadCategoryInfo];
    
     [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PublishRentInfoCell class]) bundle:nil] forCellReuseIdentifier:[PublishRentInfoCell identifierStr]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PublishRentFirstCell class]) bundle:nil] forCellReuseIdentifier:[PublishRentFirstCell identifierStr]];

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PublishRentSetionCell class]) bundle:nil] forCellReuseIdentifier:[PublishRentSetionCell identifierStr]];

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PublishRentImagesCell class]) bundle:nil] forCellReuseIdentifier:[PublishRentImagesCell identifierStr]];

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PublishRentDesCell class]) bundle:nil] forCellReuseIdentifier:[PublishRentDesCell identifierStr]];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tagsDidFilishSelet) name:@"TagsDidFilishSelet" object:nil];

    [self setNav];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WXApiDidSuccessNotification) name:WXApiDidSuccessNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WXApiDidFailNotification) name:WXApiDidFailNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AlipayDidSuccessNotification) name:AlipayDidSuccessNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AlipayDidFailNotification) name:AlipayDidFailNotification object:nil];
    
    if (self.identifiStr != nil) {
        if (self.parameterModel.house_pic_array.count > 0) {
            [self cacheImages];
        }
    }
    
}


- (void)cacheImages{
    
    
    dispatch_group_t group = dispatch_group_create();
    
    for (NSString *imageStr in self.parameterModel.house_pic_array) {
        
        
        
        dispatch_group_enter(group);
        
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:imageStr] options:kNilOptions progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            dispatch_group_leave(group);
            
        }];
        
        
        
    }
    
    
    MJWeakSelf;
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        NSMutableArray<UIImage *> *arry = [NSMutableArray array];
        for (NSString *imageStr in weakSelf.parameterModel.house_pic_array) {
            
             UIImage *image0 = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:imageStr];
            
            if (image0) {
                [arry addObject:image0];

            }
            
        }
        
        if (arry.count > 0) {
            
            weakSelf.parameterModel.selectImages = arry;
            
            [weakSelf.tableView reloadData];

        }

        
        
    });
    
    
    
}




- (void)WXApiDidSuccessNotification{
    
    [SVProgressHUD showSuccessWithStatus:@"支付成功!"];
    
    if (self.identifiStr == nil) {
        

        
        [self performSelector:@selector(dissVC) withObject:nil afterDelay:2.0];
        
        
        
        
    } else {
        
        
        [self performSelector:@selector(popVC) withObject:nil afterDelay:2.0];

        
        
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"DidXiuGaiFangYuang" object:nil];
    }
    
    
}


- (void)dissVC{
    
    [self.navigationController popViewControllerAnimated:YES];

//    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)popVC{
    
    
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)WXApiDidFailNotification{
    
    [SVProgressHUD showInfoWithStatus:@"支付失败!"];
    
    
}

- (void)AlipayDidSuccessNotification{
    
    [SVProgressHUD showSuccessWithStatus:@"支付成功!"];
    
    
    
    if (self.identifiStr == nil) {
        
        
        
        [self performSelector:@selector(dissVC) withObject:nil afterDelay:2.0];
        
        
        
        
    } else {
        
        
        [self performSelector:@selector(popVC) withObject:nil afterDelay:2.0];
        
        
        
        //        [[NSNotificationCenter defaultCenter]postNotificationName:@"DidXiuGaiFangYuang" object:nil];
    }
    
    
}


- (void)AlipayDidFailNotification{
    
    [SVProgressHUD showInfoWithStatus:@"支付失败!"];
    
    
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


- (void)tagsDidFilishSelet{
    NSMutableString *housId = [NSMutableString string];
    NSMutableString *housName = [NSMutableString string];

    for (HouseParameSub *sub in self.parameterModel.language) {
        if ([sub.isSelect isEqualToString:@"1"]) {
            [housId appendString:sub.class_id];
            [housId appendString:@","];
            [housName appendString:sub.name];
            [housName appendString:@","];
        }
    }
    
    
    if (housName.length > 0) {
        
        self.parameterModel.house_language = [housId substringToIndex:housId.length - 1];
        self.parameterModel.house_language_name = [housName substringToIndex:housName.length - 1];
        
        NSLog(@"%@+++++++%@",housId,self.parameterModel.house_language);
        NSLog(@"%@+++++++%@",housName,self.parameterModel.house_language_name);
        
        [self.tableView reloadData];
    }
    
   
    
    
}

- (void)loadCategoryInfo{
    
    if (self.parameterModel != nil) {
        return;
    }
    
    
    if (myViewModel.parameterModel != nil) {
        
        self.parameterModel = myViewModel.parameterModel;
        
        [self.tableView reloadData];

    } else {
        
        
        MJWeakSelf;
        [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"house/getClassList" parameters:nil completionHandler:^(id responseObject, NSError *error) {
            
            if (!error) {
                
                //            MYJSONSTR
                
                weakSelf.parameterModel = [HouseParameterModel mj_objectWithKeyValues:responseObject[@"data"]];
                
                for (HouseParameSub *sub in weakSelf.parameterModel.tag) {
                    sub.isSelect = @"0";
                }
                
                weakSelf.parameterModel.house_tag_name = @"最多可选三个";
                
                [weakSelf.tableView reloadData];
                
            } else {
                
                [SVProgressHUD showInfoWithStatus:@"网络异常,稍后再试!"];
            }
        }];
        
        
        
    }
    
   
    
}


- (void)setNav{
    
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateHighlighted];
    
    [but addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    but.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    //        but.backgroundColor = [UIColor redColor];
    
    but.frame = CGRectMake(0, 0, 44, 44);
    but.contentEdgeInsets = UIEdgeInsetsMake(11, -10, 11, 22);
    
    UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    self.navigationItem.leftBarButtonItem = returnButtonItem;
    
    
    if (self.identifiStr == nil) {
        
        

        self.navigationItem.title = @"发布租房信息";
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(publishBtnDidTap)];
        

    } else {
        
        
        self.navigationItem.title = @"修改租房信息";
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(publishBtnDidTap)];

    }
    

    
}


- (void)back{
    
    [self.view endEditing:YES];
    
    if (self.identifiStr == nil) {
        [self.navigationController popViewControllerAnimated:YES];

//        [self dismissViewControllerAnimated:YES completion:nil];

    } else {
        
        [self.navigationController popViewControllerAnimated:YES];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"DidXiuGaiFangYuang" object:nil];
    }
    

    
    
}

- (void)setParameterModel:(HouseParameterModel *)parameterModel{
    _parameterModel = parameterModel;
    
    if (self.identifiStr != nil) {
        
        if ([_parameterModel.house_price hasSuffix:@"P"]) {
            
            _parameterModel.house_price =  [_parameterModel.house_price substringToIndex:_parameterModel.house_price.length - 1];
        }
        
    }
    
    
    
}

- (void)publishBtnDidTap{
    
    [self.view endEditing:YES];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//    self.parameterModel.house_tag
    parameters[@"uid"] = myViewModel.user.uid;
//    NSLog(@"")
    
    if (self.parameterModel.house_id.length > 0) {
        
        parameters[@"house_id"] = self.parameterModel.house_id;

    }
    
    
    
    
    
    if (self.parameterModel.house_name == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写房源位置"];
        
        return;
    }
    
    parameters[@"house_name"] = self.parameterModel.house_name;

    
    if (self.parameterModel.house_price == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写房源价格"];
        
        return;
    }
    
    
    //判断是否是纯数字
    
    
    
    if([self isPureInt:self.parameterModel.house_price] || [self isPureFloat:self.parameterModel.house_price])
        
    {
        
        
        if (!(self.parameterModel.house_price.floatValue > 0)) {
            
            [SVProgressHUD showInfoWithStatus:@"房源价格不规范"];
            
            return;
        }
        
        parameters[@"house_price"] = self.parameterModel.house_price;

        
    }else{
        
        
        [SVProgressHUD showInfoWithStatus:@"租金填写不规范"];
        
        return;
        
        
    }

    
    
//    if (self.parameterModel.house_tag == nil) {
//
//        [SVProgressHUD showInfoWithStatus:@"请选择标签特色"];
//
//        return;
//    }
//
//    parameters[@"house_tag"] = self.parameterModel.house_tag;
//
    
    
    
    if (self.parameterModel.class_id == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择房源户型"];
        
        return;
    }
    
    parameters[@"class_id"] = self.parameterModel.class_id;
    
    
    
    
    if (self.parameterModel.pay_class_id == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择付款方式"];
        
        return;
    }
    parameters[@"pay_class_id"] = self.parameterModel.pay_class_id;
    
    
    
    if (self.parameterModel.house_area == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写房源面积"];
        
        return;
    }
    
    //判断是否是纯数字
    
    if([self isPureInt:self.parameterModel.house_area] || [self isPureFloat:self.parameterModel.house_area])
        
    {
        
        
        if (!(self.parameterModel.house_area.floatValue > 0)) {
            
            [SVProgressHUD showInfoWithStatus:@"面积填写不规范"];
            
            return;
        }
        
        parameters[@"house_area"] = self.parameterModel.house_area;

        
    }else{
        
        
        [SVProgressHUD showInfoWithStatus:@"面积填写不规范"];
        
        return;
        
        
    }
    
    
    
    
    if (self.parameterModel.province_id == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择房源区域"];
        
        return;
    }
    parameters[@"province_id"] = self.parameterModel.province_id;
    
    
    if (self.parameterModel.city_id == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择房源区域"];
        
        return;
    }
    parameters[@"city_id"] = self.parameterModel.city_id;
    
    if (self.parameterModel.district_id.length > 0) {
        
        parameters[@"district_id"] = self.parameterModel.district_id;

    }
    
    
    if (self.parameterModel.house_face == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择房屋朝向"];
        
        return;
    }
    parameters[@"house_face"] = self.parameterModel.house_face;
    
    
    
    if (self.parameterModel.house_floor == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写房屋楼层"];
        
        return;
    }
    parameters[@"house_floor"] = self.parameterModel.house_floor;
    
    
    
    
    if (self.parameterModel.house_decorate == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择装修程度"];
        
        return;
    }
    parameters[@"house_decorate"] = self.parameterModel.house_decorate;
    
    
    
    if (self.parameterModel.house_type == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择房源类型"];
        
        return;
    }
    parameters[@"house_type"] = self.parameterModel.house_type;
    
    
    
    if (self.parameterModel.house_details == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写详细描述"];
        
        return;
    }
    parameters[@"house_details"] = self.parameterModel.house_details;
    
    
    
    
    if (self.parameterModel.house_phone == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写手机号"];
        
        return;
    }
    
    if (![[NetworkTools shareNetworkTools]isPhoneNumber:self.parameterModel.house_phone]) {
        
        [SVProgressHUD showInfoWithStatus:@"手机号格式错误"];
        
        return;
    }
    parameters[@"house_phone"] = self.parameterModel.house_phone;
    
    
    
    if (self.parameterModel.house_language == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择交流语言"];
        
        return;
    }
    parameters[@"house_language"] = self.parameterModel.house_language;
    
    
    
    if (self.parameterModel.renting_style_id == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择租房方式"];
        
        return;
    }
    
    parameters[@"renting_style_id"] = self.parameterModel.renting_style_id;
    
    if (self.identifiStr == nil) {
        if (self.parameterModel.selectImages == nil  || self.parameterModel.selectImages.count == 0) {
            
            [SVProgressHUD showInfoWithStatus:@"请选择房源图片"];
            
            return;
        }
    }
    
    
  
    
    
//    parameters[@"house_title"] = [NSString stringWithFormat:@"%@ . %@ %@",self.parameterModel.renting_style_name,self.parameterModel.house_name,self.parameterModel.huXing_name];

    
    
//    BOOL istelephoneNumber = [[NetworkTools shareNetworkTools] isPhoneNumber:self.userTextField.text];
//    if (!istelephoneNumber) {
//        [SVProgressHUD showInfoWithStatus:@"用户名号格式不正确!"];
//        return;
//    }

    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
//    manger.responseSerializer  = [AFHTTPResponseSerializer serializer];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/plain", @"text/javascript",@"text/html" ,nil];
    
    NSString *url = @"http://feidu.baonongkang.cn/index.php/api/house/addHouse";
    
//
    
    
//      http://10.254.10.223/feidu/api/House/addHouse
    if (self.parameterModel.selectImages.count > 0) {
        
        [SVProgressHUD showWithStatus:@"图片上传中请稍等..."];
        
    }else{
        
         [SVProgressHUD showWithStatus:@"信息提交中请稍等..."];
        
    }
    
    
    
    
    MJWeakSelf
    [manger POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        if (self.parameterModel.selectImages.count > 0) {
            
            int i = 0;
            
            for (UIImage *img in self.parameterModel.selectImages) {
                
                NSData * data = UIImageJPEGRepresentation([img getScaledImage], 1.0);
                NSString *name = [NSString stringWithFormat:@"file%d",i];
                [formData appendPartWithFileData:data
                                            name:name
                                        fileName:[NSString stringWithFormat:@"%@.png",name]
                                        mimeType:@"application/octet-stream"];
                
                i++;
                
            }
            
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
//
//        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//
//        NSLog(@"完成 %@", result);
        
        MYJSONSTR;
        
        weakSelf.payMoney = responseObject[@"data"][@"pay_rmb"];
        weakSelf.payOrderNumber = responseObject[@"data"][@"out_trade_no"];
        
        
        if (self.identifiStr == nil) {
            
            [SVProgressHUD dismiss];
            [weakSelf goToPay];
            
            
        }else{
            
            [SVProgressHUD showSuccessWithStatus:@"修改成功!"];
            
           [weakSelf performSelector:@selector(back) withObject:nil afterDelay:2.0];
            
        }
        
        
//        [SVProgressHUD showSuccessWithStatus:responseObject[@"data"]];
//

        
        
//        [weakSelf dismissViewControllerAnimated:YES completion:nil];
        
//        NSLog(@"%@",[responseObject mj_JSONString]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [SVProgressHUD showErrorWithStatus:error.domain];
        
    }];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
    
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    
    
    
    
}



- (void)goToPay{
    
    MJWeakSelf;
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:@"选择支付方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    [contr addAction:action0];
    
    
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"微信支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        weakSelf.payType = @"1";
        
        [weakSelf wachatPay];
        
    }];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"支付宝支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        weakSelf.payType = @"2";

        [weakSelf alipayPayWith];

    }];
    
    
    
    [contr addAction:action1];
    [contr addAction:action2];
    
    
    [self presentViewController:contr animated:YES completion:nil];
   
    
    
    
}





- (void)alipayPayWith{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"payment"] = self.payMoney;
    parameters[@"out_trade_no"] = self.payOrderNumber;
    parameters[@"pay_type"] = self.payType;
    
    [SVProgressHUD showWithStatus:@"订单确认中..."];
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Pay/pay_order" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
            
            [SVProgressHUD dismiss];
            
            [[AlipaySDK defaultService] payOrder:responseObject[@"data"] fromScheme:@"com.beisheng.feifubao" callback:^(NSDictionary *resultDic) {
                
                
//                NSLog(@"reslut = %@",resultDic);
                
            }];
            
            
        } else {
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
        
    }];
    
    
    
}


- (void)wachatPay{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"payment"] = self.payMoney;
    parameters[@"out_trade_no"] = self.payOrderNumber;
    parameters[@"pay_type"] = self.payType;
    
    [SVProgressHUD showWithStatus:@"订单确认中..."];
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Pay/pay_order" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
            
            [SVProgressHUD dismiss];
            
//            MYJSONSTR;
            
            [self WXPayWithDic:responseObject[@"data"]];
            
            
        } else {
            
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
        
    }];
    
    
    
}




- (void)WXPayWithDic:(NSDictionary *)dic{
    
    //需要创建这个支付对象
    PayReq *req   = [[PayReq alloc] init];
    //由用户微信号和AppID组成的唯一标识，用于校验微信用户
    req.openID = dic[@"appid"];
    
    // 商家id，在注册的时候给的
    req.partnerId = dic[@"partnerid"];
    
    // 预支付订单这个是后台跟微信服务器交互后，微信服务器传给你们服务器的，你们服务器再传给你
    req.prepayId  = dic[@"prepayid"];
    
    // 根据财付通文档填写的数据和签名
    //这个比较特殊，是固定的，只能是即req.package = Sign=WXPay
    req.package   = dic[@"pkg"];
    
    // 随机编码，为了防止重复的，在后台生成
    req.nonceStr  = dic[@"noncestr"];
    
    // 这个是时间戳，也是在后台生成的，为了验证支付的
    NSString * stamp = dic[@"timestamp"];
    
    req.timeStamp = stamp.intValue;
    
    // 这个签名也是后台做的
    req.sign = dic[@"sign"];
    
    //发送请求到微信，等待微信返回onResp
    [WXApi sendReq:req];
}





//判断是否为整形：

- (BOOL)isPureInt:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    int val;
    
    return [scan scanInt:&val] && [scan isAtEnd];
    
}


//判断是否为浮点形：

- (BOOL)isPureFloat:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    float val;
    
    return [scan scanFloat:&val] && [scan isAtEnd];
    
}



#pragma mark - tableviewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return  3;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 13;
    } else {
        return 1;
    }
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0 || indexPath.row == 10) {
            
            PublishRentFirstCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[PublishRentFirstCell identifierStr]];
            tableCell.index = indexPath;
            
            tableCell.model = self.parameterModel;
            
            return tableCell;
            
        } else {
            
            PublishRentInfoCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[PublishRentInfoCell identifierStr]];
            
            tableCell.index = indexPath;
            
            tableCell.model = self.parameterModel;
            
            return tableCell;
        }
        
    } else {
        
        if (indexPath.section == 1) {
            
            PublishRentDesCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[PublishRentDesCell identifierStr]];
            
            tableCell.index = indexPath;
            
            tableCell.model = self.parameterModel;
            
            return tableCell;
  
        } else {
            
            PublishRentImagesCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[PublishRentImagesCell identifierStr]];
            
            if (self.identifiStr != nil) {
                
                tableCell.model = self.parameterModel;

            } else {
                
                tableCell.type = @"0";
                tableCell.model = self.parameterModel;

            }
            
            
            return tableCell;
     
        }
    }
    
  
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
         return 0.00001;
    }else{
         return 36;
        
    }
        
   

    
}



- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    UIView *temp = [[UIView alloc]init];
    temp.backgroundColor = [UIColor whiteColor];
    return temp;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        UIView *temp = [[UIView alloc]init];
        temp.backgroundColor = [UIColor whiteColor];
        return temp;
    } else {
        UIView *temp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 36)];
        UILabel *label = [[UILabel alloc]init];
        if (section == 1) {
            label.text = @"房源描述";
        } else {
            label.text = @"房源图片";
        }
        
        label.textColor = darkGray;
        label.font = MYFONT(16);
        [label sizeToFit];
        label.centerY = 18;
        label.left = 16;
        [temp addSubview:label];
        temp.backgroundColor = viewBgGrey;
        return temp;
    }
    
   
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.view endEditing:YES];
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
        } else if (indexPath.row == 1){
            
            [self index1DidTap];
            
        }else if (indexPath.row == 2){
            
        }else if (indexPath.row == 3){
            
            [self index3DidTap];
            
        }else if (indexPath.row == 4){
            
            [self index4DidTap];
            
        }else if (indexPath.row == 5){
            
        }else if (indexPath.row == 6){
            
            [self index6DidTap];

        }else if (indexPath.row == 7){
            
            [self index7DidTap];

        }else if (indexPath.row == 8){
            
        }else if (indexPath.row == 9){
            
            [self index9DidTap];

        }else if (indexPath.row == 10){
            
            
        }else if (indexPath.row == 11){
            
            [self index10DidTap];
            
        }else if (indexPath.row == 12){
            
            [self index13DidTap];

        }
    }
    
    
    
}



- (void)index1DidTap{

        
        SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
        
        view.isSingle = YES;
    
    NSMutableArray *tempM = [NSMutableArray array];
    
    for (int i = 0; i < self.parameterModel.rentingStyle.count; i++) {
        
        HouseParameSub *subModel = self.parameterModel.rentingStyle[i];
        [tempM addObject:[[SelectedListModel alloc] initWithSid:i Title:subModel.name]];
    }
        
        
        view.array = tempM;
        
        
        MJWeakSelf;
        view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
            
            [LEEAlert closeWithCompletionBlock:^{
                
                weakSelf.parameterModel.renting_style_name = array.firstObject.title;
                
                for (HouseParameSub *subP in self.parameterModel.rentingStyle) {
                    if ([array.firstObject.title isEqualToString:subP.name]) {
                        weakSelf.parameterModel.renting_style_id = subP.class_id;
                    }
                }
                
                [weakSelf.tableView reloadData];
                
                
                
            }];
            
        };
        
        [LEEAlert alert].config
        .LeeTitle(@"选择租赁方式")
        .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
        .LeeCustomView(view)
        .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
        .LeeClickBackgroundClose(YES)
        .LeeShow();
    
    }




- (void)index3DidTap{
    
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    NSMutableArray *tempM = [NSMutableArray array];
    
    for (int i = 0; i < self.parameterModel.fukuan.count; i++) {
        
        HouseParameSub *subModel = self.parameterModel.fukuan[i];
        [tempM addObject:[[SelectedListModel alloc] initWithSid:i Title:subModel.name]];
        
    }
    
    
    view.array = tempM;
    
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            weakSelf.parameterModel.pay_class_method = array.firstObject.title;
            
            for (HouseParameSub *subP in self.parameterModel.fukuan) {
                if ([array.firstObject.title isEqualToString:subP.name]) {
                    weakSelf.parameterModel.pay_class_id = subP.class_id;
                }
            }
            
            [weakSelf.tableView reloadData];
            
            
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择付款方式")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
    
}

    
- (void)index4DidTap{
    
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    NSMutableArray *tempM = [NSMutableArray array];
    
    for (int i = 0; i < self.parameterModel.huxing.count; i++) {
        
        HouseParameSub *subModel = self.parameterModel.huxing[i];
        [tempM addObject:[[SelectedListModel alloc] initWithSid:i Title:subModel.name]];
    }
    
    
    view.array = tempM;
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            weakSelf.parameterModel.huXing_name = array.firstObject.title;
            
            for (HouseParameSub *subP in self.parameterModel.huxing) {
                if ([array.firstObject.title isEqualToString:subP.name]) {
                    weakSelf.parameterModel.class_id = subP.class_id;
                }
            }
            
            [weakSelf.tableView reloadData];
            
            
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择房屋户型")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
    
}

- (void)index6DidTap{
    
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    NSMutableArray *tempM = [NSMutableArray array];
    
    for (int i = 0; i < self.parameterModel.houseStyle.count; i++) {
        
        HouseParameSub *subModel = self.parameterModel.houseStyle[i];
        [tempM addObject:[[SelectedListModel alloc] initWithSid:i Title:subModel.name]];
    }
    
    
    view.array = tempM;
    
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            weakSelf.parameterModel.house_type_name = array.firstObject.title;
            
            for (HouseParameSub *subP in self.parameterModel.houseStyle) {
                if ([array.firstObject.title isEqualToString:subP.name]) {
                    weakSelf.parameterModel.house_type = subP.class_id;
                }
            }
            
            [weakSelf.tableView reloadData];
            
            
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择房源类型")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
    
}




- (void)index7DidTap{
    
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    NSMutableArray *tempM = [NSMutableArray array];
    
    for (int i = 0; i < self.parameterModel.position.count; i++) {
        
        HouseParameSub *subModel = self.parameterModel.position[i];
        [tempM addObject:[[SelectedListModel alloc] initWithSid:i Title:subModel.name]];
    }
    
    
    view.array = tempM;
    
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            weakSelf.parameterModel.house_face_name = array.firstObject.title;
            
            for (HouseParameSub *subP in self.parameterModel.position) {
                if ([array.firstObject.title isEqualToString:subP.name]) {
                    weakSelf.parameterModel.house_face = subP.class_id;
                }
            }
            
            [weakSelf.tableView reloadData];
            
            
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择房屋朝向")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
    
}




- (void)index9DidTap{
    
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    NSMutableArray *tempM = [NSMutableArray array];
    
    for (int i = 0; i < self.parameterModel.decorateStyle.count; i++) {
        
        HouseParameSub *subModel = self.parameterModel.decorateStyle[i];
        [tempM addObject:[[SelectedListModel alloc] initWithSid:i Title:subModel.name]];
    }
    
    
    view.array = tempM;
    
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            weakSelf.parameterModel.house_decorate_name = array.firstObject.title;
            
            for (HouseParameSub *subP in self.parameterModel.decorateStyle) {
                if ([array.firstObject.title isEqualToString:subP.name]) {
                    weakSelf.parameterModel.house_decorate = subP.class_id;
                }
            }
            
            [weakSelf.tableView reloadData];
            
            
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择装修类型")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
    
}


- (void)index10DidTap{
//    SelectFeatureVC
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"SelectFeatureVC" bundle:[NSBundle mainBundle]];
    
    SelectFeatureVC *presentVC = [sb instantiateInitialViewController];
    
    presentVC.model = self.parameterModel;
    
    self.animedDelegate.model = self.parameterModel;
    
    presentVC.transitioningDelegate = self.animedDelegate;
    
    presentVC.modalPresentationStyle = UIModalPresentationCustom;
    /*自定义转场
     1.设置转场代理
     2.设置转场风格
     3.实现转场代理方法,返回一个继承UIPresentationController的负责转场的控制器
     */
    [self presentViewController:presentVC animated:YES completion:nil];
    
    
    
    
}

- (void)index12DidTap{
    
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    NSMutableArray *tempM = [NSMutableArray array];
    
    for (int i = 0; i < self.parameterModel.language.count; i++) {
        
        HouseParameSub *subModel = self.parameterModel.language[i];
        [tempM addObject:[[SelectedListModel alloc] initWithSid:i Title:subModel.name]];
    }
    
    
    view.array = tempM;
    
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            weakSelf.parameterModel.house_language_name = array.firstObject.title;
            
            for (HouseParameSub *subP in self.parameterModel.language) {
                if ([array.firstObject.title isEqualToString:subP.name]) {
                    weakSelf.parameterModel.house_language = subP.class_id;
                }
            }
            
            [weakSelf.tableView reloadData];
            
            
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择交流语言")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
    
}



- (void)index13DidTap{
    
    
    __block NSString *province = nil;
    
    __block NSString *city = nil;
    
    __block NSString *area = nil;
    
    
    MJWeakSelf;
    void (^resultBlock)(void) = ^{
        
        
        weakSelf.parameterModel.province = province;
        
        weakSelf.parameterModel.city = city;
        
        weakSelf.parameterModel.area = area;
        
        if (area.length > 0) {
            
            weakSelf.parameterModel.lastString = [NSString stringWithFormat:@"%@-%@-%@",province,city,area];
            
        } else {
            
            weakSelf.parameterModel.lastString = [NSString stringWithFormat:@"%@-%@",province,city];
            
        }
        
        [weakSelf.tableView reloadData];
        
        
        [weakSelf processData];
   
        
    };
    
    
    NSArray *provinceArray = self.dateArray;
    
    if (provinceArray.count) {
        
        // 获取省数据 并显示
        
        NSMutableArray *array = [NSMutableArray array];
        
        [provinceArray enumerateObjectsUsingBlock:^(NSDictionary *info, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:info[@"name"] Context:info]];
        }];
        
        SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
        
        view.isSingle = YES;
        
        view.array = array;
        
        view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
            
            [LEEAlert closeWithCompletionBlock:^{
                
                // 根据选中的省获取市数据 并显示
                
                SelectedListModel *model = array.firstObject;
                
                province = model.title; //设置省
                
                NSArray *cityArray = model.context[@"sub"];
                
                if (cityArray.count) {
                    
                    NSMutableArray *array = [NSMutableArray array];
                    
                    [cityArray enumerateObjectsUsingBlock:^(NSDictionary *info, NSUInteger idx, BOOL * _Nonnull stop) {
                        
                        [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:info[@"name"] Context:info]];
                    }];
                    
                    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
                    
                    view.isSingle = YES;
                    
                    view.array = array;
                    
                    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
                        
                        [LEEAlert closeWithCompletionBlock:^{
                            
                            // 根据选中的市获取区数据 并显示
                            
                            SelectedListModel *model = array.firstObject;
                            
                            city = model.title; //设置市
                            
                            NSArray *areaArray = model.context[@"sub"];
                            
                            if (areaArray.count) {
                                
                                NSMutableArray *array = [NSMutableArray array];
                                
                                [areaArray enumerateObjectsUsingBlock:^(NSString *area, NSUInteger idx, BOOL * _Nonnull stop) {
                                    
                                    [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:area]];
                                }];
                                
                                SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
                                
                                view.isSingle = YES;
                                
                                view.array = array;
                                
                                view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
                                    
                                    [LEEAlert closeWithCompletionBlock:^{
                                        
                                        // 获取区数据 并显示最终结果
                                        
                                        SelectedListModel *model = array.firstObject;
                                        
                                        area = model.title; //设置区
                                        
                                        //                                                NSLog(@"%@----",area);
                                        
                                        
                                        if (resultBlock) resultBlock();
                                    }];
                                    
                                };
                                
                                [LEEAlert alert].config
                                .LeeTitle(model.title)
                                .LeeCustomView(view)
                                .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
                                .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
                                .LeeClickBackgroundClose(YES)
                                .LeeShow();
                                
                            } else {
                                
                                if (resultBlock) resultBlock();
                            }
                            
                        }];
                        
                    };
                    
                    [LEEAlert alert].config
                    .LeeTitle(model.title)
                    .LeeCustomView(view)
                    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
                    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
                    .LeeClickBackgroundClose(YES)
                    .LeeShow();
                    
                } else {
                    
                    if (resultBlock) resultBlock();
                }
                
            }];
            
        };
        
        [LEEAlert alert].config
        .LeeTitle(@"请选择")
        .LeeCustomView(view)
        .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
        .LeeClickBackgroundClose(YES)
        .LeeShow();
    }
    
    
    
    
}





- (void)processData{
    
    self.parameterModel.province_id = nil;
    self.parameterModel.city_id = nil;
    self.parameterModel.district_id = nil;

    for (ProvinceModel *province in myViewModel.areas) {
        
        if ([province.province_name isEqualToString:self.parameterModel.province]) {
            
            self.parameterModel.province_id = province.province_id;
            
            for (CitysModel *city in province.citys) {
                
                if ([city.city_name isEqualToString:self.parameterModel.city]){
                    self.parameterModel.city_id = city.city_id;
                    
                    for (DistrictsModel *district in city.districts) {

                        if ([district.district_name isEqualToString:self.parameterModel.area]) {
                            self.parameterModel.district_id = district.district_id;
                        }
                    }
                    
                }
                
            }
            
            //            NSLog(@"%@===%@===%@",self.provinceId,self.cityId,self.districtId);
            return;
        }
        
        
        
    }
    
    
    
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
